import {request, handleError} from "@/api/request";

const BASE_URL = "http://127.0.0.1:8088";

/**
 * 获取总人数
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getPatientTotal = async () => {
    try {
        const res = await request.get(BASE_URL + '/patient/get_patient_total')
        return res
    } catch (err) {
        handleError(err)
    }
}

/**
 * 获取挂号医院分布比例和挂号医院排行
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getDistributionOfHospital = async () => {
    try {
        const res = await request.get(BASE_URL + '/patient/getDistributionOfHospital')
        return res
    } catch (err) {
        handleError(err)
    }
}

/**
 * 获取男女分布比例
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getDistributionOfSex = async () => {
    try {
        const res = await request.get(BASE_URL + '/patient/getDistributionOfSex')
        return res
    } catch (err) {
        handleError(err)
    }
}

/**
 * 获取挂号类型分布比例和挂号类型排行
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getDistributionOfDepartment = async () => {
    try {
        const res = await request.get(BASE_URL + '/patient/getDistributionOfDepartment')
        return res
    } catch (err) {
        handleError(err)
    }
}


/**
 * 获取最高身高,最低身高,平均身高
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getPatientHeightStats= async () => {
    try {
        const res = await request.get(BASE_URL + '/patient/get_patient_height_stats')
        return res
    } catch (err) {
        handleError(err)
    }
}

/**
 * 获取最高体重,最低体重,平均体重
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getPatientWeightStats= async () => {
    try {
        const res = await request.get(BASE_URL + '/patient/get_patient_weight_stats')
        return res
    } catch (err) {
        handleError(err)
    }
}


/**
 * 获取最高年龄,最低年龄,平均年龄
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getPatientAgeStats= async () => {
    try {
        const res = await request.get(BASE_URL + '/patient/get_patient_age_stats')
        return res
    } catch (err) {
        handleError(err)
    }
}


/**
 * 获取病患列表
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const getPatientList = async () => {
    try {
        const res = await request.get(BASE_URL + '/patient/get_patient_list')
        return res
    } catch (err) {
        handleError(err)
    }
}


/**
 * 编辑患者信息
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const editPatientInfo = async (id, param) => {
    try {
        const res = await request.put(BASE_URL + `/patient/edit_patient/${id}`, param)
        return res
    } catch (err) {
        handleError(err)
    }
}


/**
 * 删除病患信息
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const deletePatientInfo = async (id) => {
    try {
        const res = await request.delete(BASE_URL + `/patient/delete/${id}`)
        return res
    } catch (err) {
        handleError(err)
    }
}

/**
 * 导出病患信息
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const exportPatientInfo = async () => {
    try {
        const res = await request.get(BASE_URL + '/patient/export_patients')
        return res
    } catch (err) {
        handleError(err)
    }
}

/**
 * 患者登录
 * @param {String} account 账号
 * @param {String} password 密码
 * @returns {Object} res 返回结果
 */
export const patientLogin = async (account, password) => {
    try {
        const param = new URLSearchParams();
        param.append("account", account);
        param.append("password", password);

        const res = await request.post(BASE_URL + "/patient/login", param);
        return res;
    } catch (err) {
        handleError(err);
    }
};
